<?php
function CutStr($str,$number = 150,$dot=true)
{
    $leng = strlen($str);
    if($leng > $number)
    {
        $temp  = substr($str, 0, $number);
        $str   = substr($temp, 0, strrpos($temp, " ")).($dot ? "..." : "");
    }

    return $str;
}
/**
* kiểm tra trạng thái login
*/
function check_login_user()
{
    $ci = & get_instance();
    $info_user= $ci->session->userdata('uinfo');
    return (isset($info_user) AND $info_user!='')?true:false;
}

function user_online()
{
    $tb_user_online = 'user_online';
    $ci = & get_instance();
    $user_id = -1;
    $username = '';
    //var_dump($ci->session->userdata('uinfo'));
    if($ci->session->userdata('uinfo'))
    {
        $user_id = info_from_session_user(0);
        $username = info_from_session_user(1);
    }
    $arr = array(
        'session_id'    =>  $ci->session->userdata('session_id'),
        'user_id'       =>  $user_id,
        'username'      =>  $username,
        'last_activity' =>  time(),
        'ip_address'    =>  $ci->input->ip_address()
    );
    $time_sesion = intval(TIME_SESSION);
    $time_live = time()-$time_sesion;
    $sql = 'DELETE FROM '.$tb_user_online.' WHERE (last_activity < '.$time_live.') OR (user_id = -1 AND session_id = "'.$ci->session->userdata('session_id').'")';
    $ci->db->query($sql);
    $sql = 'SELECT * FROM '.$tb_user_online.' WHERE session_id = "'.$ci->session->userdata('session_id').'"';
    if($ci->db->query($sql)->num_rows() > 0){
    $ci->db->update($tb_user_online,$arr,array('last_activity'=>time()));
    }
    else{
    $ci->db->insert($tb_user_online,$arr);
    }

//$ci->user_model->user_online();
}
/**
 * tách thông tin trong session
 */
function info_from_session_user($position = 1)
{
    $ci = & get_instance();
    $info= $ci->session->userdata('uinfo');
    $arr = explode("|",$info);
    return isset($arr[$position]) ? $arr[$position] : false;
}
function items_link($str)
{
    return base_url().'admin'."/".$str;
}